//
//  ST_TabbarViewController.m
//  ST_BaseMall
//
//  Created by 昱胜 on 2022/9/2.
//

#import "ST_TabbarViewController.h"
#import "ST_BaseNavigationController.h"
#import "HNSH_HomeManager.h"
//#import "HNSH_TongZhengViewController.h"
//#import "HNSH_BusinessCircleManager.h"      
#import "HNSH_XFBManage.h"
#import "HNSH_MineViewController.h"
#import "HNSH_CartShopViewController.h"

@interface ST_TabbarViewController ()<AxcAE_TabBarDelegate>

@end

@implementation ST_TabbarViewController

+ (ST_TabbarViewController *)shareTabbar {
    static ST_TabbarViewController *tabbar = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tabbar = [[ST_TabbarViewController alloc]init];
    });
    return tabbar;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 添加子VC
    [self addChildViewControllers];
    
    [self configChangeLine];
}

//改变tabbar顶部线的颜色
- (void)configChangeLine {
    CGRect rect = CGRectMake(0, 0, kScreenWidth, 0.5);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,[UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
    [self.tabBar setBackgroundImage:[[UIImage alloc]init]];
}

- (void)addChildViewControllers{
    // 创建选项卡的数据 想怎么写看自己，这块我就写笨点了
    NSArray <NSDictionary *>*VCArray =
    @[@{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_HomeManager new]],
        @"normalImg":@"tabbar_home",
        @"selectImg":@"tabbar_home_select",
        @"itemTitle":@"首页"},
      @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_CartShopViewController new]],
        @"normalImg":@"tabbar_cart",
        @"selectImg":@"tabbar_cart_select",
        @"itemTitle":@"购物车"},
      @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_MineViewController new]],
        @"normalImg":@"tabbar_mine",
        @"selectImg":@"tabbar_mine_select",
        @"itemTitle":@"我的"}];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
        VCArray =
        @[@{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_HomeManager new]],
            @"normalImg":@"tabbar_home",
            @"selectImg":@"tabbar_home_select",
            @"itemTitle":@"首页"},
          @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_XFBManage new]],
            @"normalImg":@"tabbar_quan",
            @"selectImg":@"tabbar_quan_select",
            @"itemTitle":@"消费包"},
//          @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_TongZhengViewController new]],
//              @"normalImg":@"tabbar_zheng",
//              @"selectImg":@"tabbar_zheng_select",
//              @"itemTitle":@"通证"},
          @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_CartShopViewController new]],
            @"normalImg":@"tabbar_cart",
            @"selectImg":@"tabbar_cart_select",
            @"itemTitle":@"购物车"},
          @{@"vc":[[ST_BaseNavigationController alloc]initWithRootViewController:[HNSH_MineViewController new]],
            @"normalImg":@"tabbar_mine",
            @"selectImg":@"tabbar_mine_select",
            @"itemTitle":@"我的"}];
    }
    // 1.遍历这个集合
    // 1.1 设置一个保存构造器的数组
    NSMutableArray *tabBarConfs = @[].mutableCopy;
    // 1.2 设置一个保存VC的数组
    NSMutableArray *tabBarVCs = @[].mutableCopy;
    [VCArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 2.根据集合来创建TabBar构造器
        AxcAE_TabBarConfigModel *model = [AxcAE_TabBarConfigModel new];
        // 3.item基础数据三连
        model.itemTitle = [obj objectForKey:@"itemTitle"];
        model.selectImageName = [obj objectForKey:@"selectImg"];
        model.normalImageName = [obj objectForKey:@"normalImg"];
        // 4.设置单个选中item标题状态下的颜色
        model.selectColor = [UIColor appGreenColor];
        model.normalColor = [UIColor appGrayLabelColor];
        
        // 备注 如果一步设置的VC的背景颜色，VC就会提前绘制驻留，优化这方面的话最好不要这么写
        // 示例中为了方便就在这写了
        ST_BaseNavigationController *vc = [obj objectForKey:@"vc"];
        // 5.将VC添加到系统控制组
        [tabBarVCs addObject:vc];
        // 5.1添加构造Model到集合
        [tabBarConfs addObject:model];
    }];
    // 5.2 设置VCs -----
    // 一定要先设置这一步，然后再进行后边的顺序，因为系统只有在setViewControllers函数后才不会再次创建UIBarButtonItem，以免造成遮挡
    // 大意就是一定要让自定义TabBar遮挡住系统的TabBar
    self.viewControllers = tabBarVCs;
    //////////////////////////////////////////////////////////////////////////
    // 注：这里方便阅读就将AE_TabBar放在这里实例化了 使用懒加载也行
    // 6.将自定义的覆盖到原来的tabBar上面
    // 这里有两种实例化方案：
    // 6.1 使用重载构造函数方式：
//    self.axcTabBar = [[AxcAE_TabBar alloc] initWithTabBarConfig:tabBarConfs];
    // 6.2 使用Set方式：
    self.axcTabBar = [AxcAE_TabBar new] ;
    self.axcTabBar.tabBarConfig = tabBarConfs;
    self.axcTabBar.backgroundColor = [UIColor appWhiteLabelColor];
    // 7.设置委托
    self.axcTabBar.delegate = self;
    // 8.添加覆盖到上边
    [self.tabBar addSubview:self.axcTabBar];
    [self addLayoutTabBar]; // 10.添加适配
}
// 9.实现代理，如下：
- (void)axcAE_TabBar:(AxcAE_TabBar *)tabbar selectIndex:(NSInteger)index{
    // 通知 切换视图控制器
    [self setSelectedIndex:index];
    // 自定义的AE_TabBar回调点击事件给TabBarVC，TabBarVC用父类的TabBarController函数完成切换
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    if (selectedIndex == 1 || selectedIndex == 3) {
        if (![ST_UserManager alreadyLogin]) {
            [ST_UserManager gotoLogin];
            return;
        }
    }
    [super setSelectedIndex:selectedIndex];
    if(self.axcTabBar){
        self.axcTabBar.selectIndex = selectedIndex;
    }
}

//+ (void)changeSelectIndex:(NSUInteger)selectedIndex {
//    YSTabbarViewController *tabbar = (YSTabbarViewController *)[UIApplication sharedApplication].delegate.window.rootViewController;
//    tabbar.axcTabBar.selectIndex = selectedIndex;
//}
//
//+ (void)changeSelectIndexAndChangeSelectItem:(NSUInteger)selectedItemIndex {
//    YSTabbarViewController *tabbar = (YSTabbarViewController *)[UIApplication sharedApplication].delegate.window.rootViewController;
//    tabbar.axcTabBar.selectIndex = 2;
//    BaseNavigationController *nav = (BaseNavigationController *)tabbar.viewControllers[2];
//    YSClientManage *vc = nav.viewControllers.firstObject;
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5), dispatch_get_main_queue(), ^{
//        vc.changeIndex = selectedItemIndex;
//    });
//}

// 10.添加适配
- (void)addLayoutTabBar{
    // 使用重载viewDidLayoutSubviews实时计算坐标 （下边的 -viewDidLayoutSubviews 函数）
    // 能兼容转屏时的自动布局
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.axcTabBar.frame = self.tabBar.bounds;
    [self.axcTabBar viewDidLayoutItems];
}

@end
